home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 135_01.zip / RATC.C < prev    next >
Text File  |  1993-06-10  |  2KB  |  185 lines

  1. /*
  2.     ratc.c---rational version of the VLI math package
  3.     for BDSc.  Usage is as in example...
  4.  
  5.     by Hugh S. Myers
  6.  
  7.     build:
  8.         n>cc ratc
  9.         n>clink ratc vli math
  10.  
  11.     4/1/84
  12.     4/9/84
  13. */
  14.  
  15. #include <bdscio.h>
  16. #define MAX 256
  17.  
  18. main()
  19. {
  20.     char s1[MAX],s2[MAX];
  21.     int i;
  22.  
  23.     forever {
  24.         puts("? ");
  25.         getline(s1,MAX);
  26.         puts("? ");
  27.         getline(s2,MAX);
  28.         printf("s1+s2= %s\n",radd(s1,s2));
  29.         printf("s1-s2= %s\n",rsub(s1,s2));
  30.         printf("s1*s2= %s\n",rmul(s1,s2));
  31.         printf("s1/s2= %s\n",rdiv(s1,s2));
  32.     }
  33. }
  34.  
  35. char *radd(t1,t2)
  36. char *t1,*t2;
  37. {
  38.     char s1[MAX],s2[MAX];
  39.     int i;
  40.  
  41.     strcpy(s1,t1);
  42.     strcpy(s2,t2);
  43.     i=scale(s1,s2);
  44.     strcpy(s1,addl(s1,s2));
  45.     insertch(s1,i,'.');
  46.     return s1;
  47. }
  48.  
  49. char *rsub(t1,t2)
  50. char *t1,*t2;
  51. {
  52.     char s1[MAX],s2[MAX];
  53.     int i;
  54.  
  55.     strcpy(s1,t1);
  56.     strcpy(s2,t2);
  57.     i=scale(s1,s2);
  58.     strcpy(s1,subl(s1,s2));
  59.     insertch(s1,i,'.');
  60.     return s1;
  61. }
  62.  
  63. char *rmul(t1,t2)
  64. char *t1,*t2;
  65. {
  66.     char s1[MAX],s2[MAX];
  67.     int i;
  68.  
  69.     strcpy(s1,t1);
  70.     strcpy(s2,t2);
  71.     i=scale(s1,s2);
  72.     strcpy(s1,mull(s1,s2));
  73.     insertch(s1,i+i,'.');
  74.     return s1;
  75. }
  76.  
  77. char *rdiv(t1,t2)
  78. char *t1,*t2;
  79. {
  80.     char s1[MAX],s2[MAX];
  81.     int i,j;
  82.  
  83.     strcpy(s1,t1);
  84.     strcpy(s2,t2);
  85.     i=scale(s1,s2);
  86.     j=max(strlen(s1),strlen(s2));
  87.     pad(s1,j);
  88.     strcpy(s1,divl(s1,s2));
  89.     insertch(s1,j,'.');
  90.     return s1;
  91. }
  92.  
  93. scale(s1,s2)
  94. char *s1,*s2;
  95. {
  96.     int i,j;
  97.     i=norm(s1);
  98.     j=norm(s2);
  99.     if(i>j) {
  100.         pad(s2,i-j);
  101.         j=i;
  102.     }
  103.     if(j>i) {
  104.         pad(s1,j-i);
  105.         i=j;
  106.     }
  107.     return i;
  108. }
  109.  
  110. norm(s)
  111. char s[];
  112. {
  113.     int i;
  114.  
  115.     if((i=israt(s))<0) {
  116.         pad(s,1);
  117.         return 1;
  118.     }
  119.     deletech(s,i);
  120.     if(i==strlen(s))
  121.         pad(s,1);
  122.     return(strlen(s,1)-i);
  123. }
  124.  
  125. israt(s)
  126. char s[];
  127. {
  128.     int i;
  129.  
  130.     for(i=0;i<strlen(s);i++)
  131.         if(s[i]=='.') {
  132.             return i;
  133.         }
  134.     return -1;
  135. }
  136.  
  137. deletech(s,n)
  138. char s[];
  139. int n;
  140. {
  141.     for(;n<strlen(s);n++)
  142.         s[n]=s[n+1];
  143. }
  144.  
  145. insertch(s,n,ch)
  146. char s[],ch;
  147. int n;
  148. {
  149.     int i;
  150.  
  151.     if(n<strlen(s)) {
  152.         n = strlen(s)-n;
  153.         for(i=strlen(s)+1;i>n;i--)
  154.             s[i]=s[i-1];
  155.         s[n]=ch;
  156.         return;
  157.     }
  158.     if(n>strlen(s))
  159.         prefix(s,n-strlen(s),'0');
  160.     prefix(s,1,'.');
  161.     return;        
  162. }
  163.  
  164. pad(s,n)
  165. char *s;
  166. int n;
  167. {
  168.     for(;n>0;n--)
  169.         strcat(s,"0");
  170. }
  171.  
  172. prefix(t,n,ch)
  173. char *t,ch;
  174. int n;
  175. {
  176.     char s[MAX];
  177.     int i;
  178.  
  179.     for(i=0;n>0;n--,i++)
  180.         s[i]=ch;
  181.     s[i]='\0';
  182.     strcat(s,t);
  183.     strcpy(t,s);
  184.     return;
  185. }